package ca.etsmtl.log320lab1;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

public class FileUtil {

	private static FileUtil _fileUtil;
	private File _currentFile;
	private FileReader _currentFileReader;
	private FileWriter _currentFileWriter;
	private ObjectOutputStream _currentOutputStream;
	private ObjectInputStream _currentInputStream;

	private FileUtil() {
	}

	public static FileUtil getInstance() {
		if (_fileUtil == null)
			_fileUtil = new FileUtil();
		return _fileUtil;
	}

	/**
	 * Gives access to the current File instance of this class.
	 * 
	 * @return
	 */
	public File getCurrentFile() {
		return _currentFile;
	}

	/**
	 * Gives access to the current FileReader instance of this class.
	 * 
	 * @return
	 */
	public FileReader getCurrentFileReader() {
		return _currentFileReader;
	}
	
	/**
	 * Gives access to the current FileReader instance of this class.
	 * 
	 * @return
	 */
	public FileWriter getCurrentFileWriter() {
		return _currentFileWriter;
	}
	
	/**
	 * Gives access to the current FileReader instance of this class.
	 * 
	 * @return
	 */
	public ObjectInputStream getOutInputStream() {
		return _currentInputStream;
	}
	/**
	 * Gives access to the current FileWriter instance of this class.
	 * 
	 * @return
	 */
	public ObjectOutputStream getCurrentOutputStream() {
		return _currentOutputStream;
	}

	/**
	 * Opens a new File instance with the provided path.
	 * 
	 * @param path
	 * @return
	 */
	public File open(String path) {
		return _currentFile = new File(path);
	}

	/**
	 * Opens the Reader of the current FileReader. Returns null if the File
	 * instance is null.
	 * 
	 * @throws FileNotFoundException
	 */
	public FileReader openReader() throws FileNotFoundException {
		if (_currentFile != null)
			return _currentFileReader = new FileReader(_currentFile);
		return null;
	}
	
	/**
	 * Opens the Reader of the current FileReader. Returns null if the File
	 * instance is null.
	 * @throws IOException 
	 */
	public FileWriter openWriter() throws IOException {
		if (_currentFile != null)
			return _currentFileWriter = new FileWriter(_currentFile);
		return null;
	}
	
	/**
	 * Opens a reader directly from the provided path.
	 * 
	 * @param path
	 * @return
	 * @throws FileNotFoundException
	 */
	public FileReader openReader(String path) throws FileNotFoundException {
		return _currentFileReader = new FileReader(
				_currentFile = new File(path));
	}
	
	/**
	 * Opens a reader directly from the provided path.
	 * 
	 * @param path
	 * @return
	 * @throws IOException 
	 */
	public FileWriter openWriter(String path) throws IOException {
		return _currentFileWriter = new FileWriter(
				_currentFile = new File(path));
	}
	/**
	 * Gets a writer from the File instance.
	 * 
	 * @return
	 * @throws IOException
	 */
	public ObjectOutputStream openOutputStream() throws IOException {
		if (_currentFile != null)
		{
			OutputStream outputStream = new FileOutputStream(_currentFile);
			return _currentOutputStream = new ObjectOutputStream(outputStream);
		}
		return null;
	}
	
	/**
	 * Gets a writer from the File instance.
	 * 
	 * @return
	 * @throws IOException
	 */
	public ObjectInputStream openInputStream() throws IOException {
		if (_currentFile != null)
		{
			InputStream inputStream = new FileInputStream(_currentFile);
			return _currentInputStream = new ObjectInputStream(inputStream);
		}
		return null;
	}

	/**
	 * Returns a writer directly from the specified path.
	 * 
	 * @param path
	 * @return
	 * @throws IOException
	 */
	public ObjectOutputStream openOutputStream(String path) throws IOException {
		_currentFile = new File(path);
		OutputStream outputStream = new FileOutputStream(_currentFile);
		return _currentOutputStream = new ObjectOutputStream(outputStream);
	}
	
	/**
	 * Returns a writer directly from the specified path.
	 * 
	 * @param path
	 * @return
	 * @throws IOException
	 */
	public ObjectInputStream openInputStream(String path) throws IOException {
		_currentFile = new File(path);
		InputStream inputStream = new FileInputStream(_currentFile);
		return _currentInputStream = new ObjectInputStream(inputStream);
	}

	/**
	 * Close without saving the file.
	 */
	public void close() {
		if (_currentFileReader != null) {
			try {
				_currentFileReader.close();
			} catch (IOException e) {
				System.out.println(e.getMessage());
			}
		}
		if (_currentFileWriter != null) {
			try {
				_currentFileWriter.close();
			} catch (IOException e) {
				System.out.println(e.getMessage());
			}
		}
		if (_currentOutputStream != null) {
			try {
				_currentOutputStream.close();
			} catch (IOException e) {
				System.out.println(e.getMessage());
			}
		}
		if (_currentInputStream != null) {
			try {
				_currentInputStream.close();
			} catch (IOException e) {
				System.out.println(e.getMessage());
			}
		}
	}
}
